using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
using WP.DB;
using WP.Modules;

namespace WP.MF.Form
{
    public partial class frmMFieldsDictMtn:BaseMtn
    {
        private DBAccess dbAccess = null;
        private bool mblnListEndFill = false;
        public frmMFieldsDictMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            dbAccess = new DBAccess(false, false);
            this.MinimumSize = this.Size;
        }

        private void frmMFieldsDictMtn_Load(object sender, EventArgs e)
        {
            ShowLanguageMenu();
            InitControl();
        }
        private void frmMFieldsDictMtn_Resize(object sender, EventArgs e)
        {
            dgList.Focus();
        }
        private void frmMFieldsDictMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            dbAccess.CloseCon();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void dgList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strValue = string.Empty;
            if (dgList.Rows.Count > 0 && mblnListEndFill)
            {
                strValue = dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (strValue.Length > 100)
                {
                    dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = strValue.Substring(0, 100);
                }
                UpdateChange(e.RowIndex, e.ColumnIndex);
            }
        }
        private void cboTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillList();
        }
        private void cboField_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillList();
        }
        private void LanguageMenuItem_Click(object sender, EventArgs e)
        {
            btnLanguage.ShowDropDown();
            ToolStripMenuItem ToolMenu = (ToolStripMenuItem)sender;
            ToolMenu.Checked = (ToolMenu.Checked ? false : true);
            if (cboField.Text != "All Fields")
            {
                dgList.Columns[ToolMenu.Text.Trim() + "_" + cboField.Text.Trim()].Visible = ToolMenu.Checked;
            }
            else
            {
                for (int i = 0; i < cboField.Items.Count - 1; i++)
                {
                    dgList.Columns[ToolMenu.Text.Trim() + "_" + cboField.Items[i].ToString()].Visible = ToolMenu.Checked;
                }
            }

        }

        public void ShowForm()
        {
            this.MdiParent = SysVar.MDIParentForm;
            this.Show();
        }
        private void InitControl()
        {
            string strSql = string.Empty;
            strSql = "select distinct TableName from mnamedict order by TableName";
            DataTable DtTable = dbAccess.GetDataTable(strSql);
            cboTable.Items.Clear();
            cboField.SelectedIndex = 0;
            foreach (DataRow DrTable in DtTable.Rows)
            {
                cboTable.Items.Add(DrTable["TableName"].ToString().Trim());   
            }
            if (cboTable.Items.Count > 0)
            {
                cboTable.SelectedIndex = 0;
            }
            else
            {
                dgList.DataSource = null;
            }
            cboField.SelectedIndex = 0;
        }
        private void AdjustGrid()
        {
            this.dgList.RowHeadersVisible = false;
            this.dgList.ColumnHeadersHeight = 22;
            this.dgList.Columns["RecID"].Visible = false;
            this.dgList.Columns["RecID"].Width = 0;
            this.dgList.Columns["RecID"].Frozen = true;
            this.dgList.Columns["FieldName"].ReadOnly = true;
            this.dgList.Columns["FieldName"].Frozen = true;
            this.dgList.Columns["FieldName"].DefaultCellStyle.BackColor = System.Drawing.SystemColors.Control;
            if (cboField.Text != "All Fields")
            {
                this.dgList.Columns[cboField.Text].ReadOnly = true;
            }
            else
            {
                for (int i = 0; i < cboField.Items.Count - 1; i++)
                {
                    this.dgList.Columns[cboField.Items[i].ToString()].ReadOnly = true;
                }
            }
            for (int i = 0; i <= dgList.Rows.Count - 1; i++)
            {
                dgList.Rows[i].Height = 18;
            }
            int intGridWidth = 0;
            for (int i = 1; i <dgList.Columns.Count; i++)
            {
                dgList.Columns[i].Width = 160;
                intGridWidth += dgList.Columns[i].Width;
            }
            if (intGridWidth < this.Width)
            {
                int intLastColumnWidth = 0;
                for (int i = 1; i < dgList.Columns.Count - 1; i++)
                {
                    intLastColumnWidth += dgList.Columns[i].Width;
                }
                dgList.Columns[dgList.Columns.Count - 1].Width = this.Width - intLastColumnWidth-18;
            }
        }
        private bool UpdateChange(int irowNo, int iColNo)
        {
            int intText = 0;
            int intColumnIndex = 0;
            string strSql = string.Empty;
            string strLanguage = string.Empty;
            string strColumnName = string.Empty;
            string strField = string.Empty;
            intColumnIndex = dgList.Rows[irowNo].Cells[iColNo].ColumnIndex;
            strSql = "select language,languageSeqNo from mLanguage where Language<>'English' order by LanguageSeqNo";
            DataTable DtLanguage = dbAccess.GetDataTable(strSql);
            foreach (DataRow DrLanguage in DtLanguage.Rows)
            {
                strColumnName = dgList.Columns[intColumnIndex].Name;
                strLanguage = DrLanguage["language"].ToString().Trim();
                string ColumnName = strColumnName.Substring(0, strLanguage.Length);
                if (DrLanguage["language"].ToString().Trim() == ColumnName)
                {
                    intText = CvtFunc.ToInt(DrLanguage["languageSeqNo"]);
                    strField = strColumnName.Substring(ColumnName.Length + 1);
                }
            }
            strSql = "update mnamedict set Language" + intText.ToString() + strField + "='"+ GetLanguageValue("Language" + intText.ToString() + strField, dgList.Rows[irowNo].Cells[iColNo].Value.ToString().Trim(), dgList.Rows[irowNo].Cells[0].Value.ToString()) + "' where RecID=" + dgList.Rows[irowNo].Cells[0].Value.ToString();
            dbAccess.ExecuteSql(strSql);
            return true;
        }
        private void FillList()
        {
            string strSql = string.Empty;
            strSql = "select * from mLanguage where Language<>'English' order by LanguageSeqNo";
            DataTable DtLanguage = dbAccess.GetDataTable(strSql);
            if (cboField.Text == "Help" || cboField.Text == "CheckEmpty")
            {
                strSql = "select recid,rtrim(ltrim(FieldName)) as FieldName,ltrim(rtrim(" + cboField.Text + ")) as " + cboField.Text + ",";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    strSql += "rtrim(ltrim(Language" + DrLanguage["LanguageSeqNo"].ToString() + cboField.Text + ")) as " + DrLanguage["Language"].ToString().Trim() + "_" + cboField.Text + ",";
                }
            }
            else if (cboField.Text == "All Fields")
            {
                strSql = "select recid,rtrim(ltrim(FieldName)) as FieldName,lTrim(rTrim(Help)) as Help,";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    strSql += "rtrim(ltrim(Language" + DrLanguage["LanguageSeqNo"].ToString() + "Help)) as " + DrLanguage["Language"].ToString().Trim() + "_Help,";
                }
                strSql += "ltrim(rtrim(CheckEmpty)) as CheckEmpty,";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    strSql += "rtrim(ltrim(Language" + DrLanguage["LanguageSeqNo"].ToString() + "CheckEmpty)) as " + DrLanguage["Language"].ToString().Trim() + "_CheckEmpty,";
                }
                strSql += "right(rtrim(ltrim(CheckExist)),len(rtrim(ltrim(CheckExist)))-CHARINDEX('|',rtrim(ltrim(CheckExist)),0)) as CheckExist,";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    string strCheckExist = "Language" + DrLanguage["LanguageSeqNo"].ToString() + "CheckExist";
                    strSql += "right(rtrim(ltrim(" + strCheckExist + ")),len(rtrim(ltrim(" + strCheckExist + ")))-CHARINDEX('|',rtrim(ltrim(" + strCheckExist + ")),0)) as " + DrLanguage["Language"].ToString().Trim() + "_CheckExist,";
                }
                strSql += "right(rtrim(ltrim(CheckRange)),len(rtrim(ltrim(CheckRange)))-CHARINDEX('|',rtrim(ltrim(CheckRange)),0)) as CheckRange,";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    string strCheckRange = "Language" + DrLanguage["LanguageSeqNo"].ToString() + "CheckRange";
                    strSql += "right(rtrim(ltrim(" + strCheckRange + ")),len(rtrim(ltrim(" + strCheckRange + ")))-CHARINDEX('|',rtrim(ltrim(" + strCheckRange + ")),0)) as " + DrLanguage["Language"].ToString().Trim() + "_CheckRange,";
                }
                strSql += "right(rtrim(ltrim(CheckUnique)),len(rtrim(ltrim(CheckUnique)))-CHARINDEX('|',rtrim(ltrim(CheckUnique)),0)) as CheckUnique,";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    string strCheckUnique = "Language" + DrLanguage["LanguageSeqNo"].ToString() + "CheckUnique";
                    strSql += "right(rtrim(ltrim(" + strCheckUnique + ")),len(rtrim(ltrim(" + strCheckUnique + ")))-CHARINDEX('|',rtrim(ltrim(" + strCheckUnique + ")),0)) as " + DrLanguage["Language"].ToString().Trim() + "_CheckUnique,";
                }
            }
            else
            {
                strSql = "select recid,rtrim(ltrim(FieldName)) as FieldName,right(rtrim(ltrim(" + cboField.Text + ")),len(rtrim(ltrim(" + cboField.Text + ")))-CHARINDEX('|',rtrim(ltrim(" + cboField.Text + ")),0)) as " + cboField.Text + ",";
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    string strField = "Language" + DrLanguage["LanguageSeqNo"].ToString() + cboField.Text;
                    strSql += "right(rtrim(ltrim(" + strField + ")),len(rtrim(ltrim(" + strField + ")))-CHARINDEX('|',rtrim(ltrim(" + strField + ")),0)) as " + DrLanguage["Language"].ToString().Trim() + "_" + cboField.Text + ",";
                }
            }
            strSql = strSql.Substring(0, strSql.Length - 1);
            strSql += " from mnamedict where TableName='" + cboTable.Text + "' order by FieldName";
            mblnListEndFill = false;
            DataTable DtFieldDict = dbAccess.GetDataTable(strSql);
            dgList.DataSource = DtFieldDict;
            AdjustGrid();
            mblnListEndFill = true;
        }
        private void ShowLanguageMenu()
        {
            DataView dv = dbAccess.GetDataTable("select * from mLanguage").DefaultView;
            dv.RowFilter = "LanguageSeqNo=2";
            if (dv.Count != 0)
            {
                language2ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language2ToolStripMenuItem.Checked = true;
                language2ToolStripMenuItem.Visible = true;
            }
            else
            {
                language2ToolStripMenuItem.Checked = false;
                language2ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=3";
            if (dv.Count != 0)
            {
                language3ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language3ToolStripMenuItem.Checked = true;
                language3ToolStripMenuItem.Visible = true;
            }
            else
            {
                language3ToolStripMenuItem.Visible = false;
                language3ToolStripMenuItem.Checked = false;
            }
            dv.RowFilter = "LanguageSeqNo=4";
            if (dv.Count != 0)
            {
                language4ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language4ToolStripMenuItem.Checked = true;
                language4ToolStripMenuItem.Visible = true;
            }
            else
            {
                language4ToolStripMenuItem.Checked = false;
                language4ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=5";
            if (dv.Count != 0)
            {
                language5ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language5ToolStripMenuItem.Checked = true;
                language5ToolStripMenuItem.Visible = true;
            }
            else
            {
                language5ToolStripMenuItem.Checked = false;
                language5ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=6";
            if (dv.Count != 0)
            {
                language6ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language6ToolStripMenuItem.Checked = true;
                language6ToolStripMenuItem.Visible = true;
            }
            else
            {
                language6ToolStripMenuItem.Checked = false;
                language6ToolStripMenuItem.Visible = false;
            }
        }
        private string GetLanguageValue(string strField,string strLanguageValue,string strRecID)
        {
            string strSql = string.Empty;
            string strValue = string.Empty;
            int intIndexOf = 0;
            if (strField.Substring(9) != "Help" && strField.Substring(9) != "CheckEmpty")
            {
                strSql = "select " + strField.Substring(9) + " from mnamedict where RecID=" + strRecID;
                strValue = dbAccess.GetValue(strSql).Trim();
                intIndexOf = strValue.IndexOf("|", 0);
                if (strLanguageValue != "")
                {
                    strValue = strValue.Substring(0, intIndexOf) + "|[" + strLanguageValue + "]";
                }
                else
                {
                    strValue = string.Empty;
                }
            }
            else
            {
                strValue = strLanguageValue;
            }
            return strValue;
        }

        
    }
}